/*
 *Copyright (c) 2008, Scendix Software, Inc.
 * 
 */
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;
using Microsoft.Win32;
using System.Diagnostics;
using System.Threading;

using mceTapiRexv2.Utils;

namespace mceTapiRexv2
{
    public class Plugin : IAddInModule, IAddInEntryPoint
    {
        public Dictionary<string, object> entryPoint;
        private TapiRexListener tRexListener;
        private Mutex mutex;
        public void Initialize(Dictionary<string, object> appInfo, Dictionary<string, object> entryPointInfo)
        {
            this.entryPoint = entryPointInfo;
            ToolsLib.LogStr("Initialize");
        }

        public void Uninitialize()
        {
        }

        public void Launch(AddInHost host)
        {
            ToolsLib.LogStr("mceTapiRex.Plugin.Launch");
            bool bMyMutexWasCreated;
            mutex = new Mutex(true, "mceTapiRexv2Mutex", out bMyMutexWasCreated);
            /*
             s_session = new HistoryOrientedPageSession();
             s_session.GoToPage("resx://mceTapiRex/mceTapiRex.Resources/Default");
             */
            // Create a named mutex to check whether an instance of this application is already running.
            if (!bMyMutexWasCreated)
            {

                host.ApplicationContext.ReturnToApplication();

            }
            else
            {                // If we get here, the application is not running; launch it now.
                if (entryPoint["ID"].ToString().ToLower() != "{39834BA8-5B7B-4cfe-8429-8AB02EF51A88}".ToLower())
                {

                    if (entryPoint.ContainsKey("Context")) // True
                    {
                        host.MediaCenterEnvironment.LaunchEntryPoint(new Guid("{9E568EE3-96A2-4de9-B1E8-0632A9E37068}"), new Guid("{39834BA8-5B7B-4cfe-8429-8AB02EF51A88}"), entryPoint["Context"].ToString());
                    }
                    else
                        host.MediaCenterEnvironment.LaunchEntryPoint(new Guid("{9E568EE3-96A2-4de9-B1E8-0632A9E37068}"), new Guid("{39834BA8-5B7B-4cfe-8429-8AB02EF51A88}"), null);
                }
               else
              {
                s_session = new HistoryOrientedPageSession();
                Application app = new Application(s_session, host);
                tRexListener = new TapiRexListener(app);
                //// Navigate to the main menu.
                if (host.EntryPointParameters.Count > 0) // True
                {
                    app.GoToPage(host.EntryPointParameters[0].ToString());
                }
                else
                    app.GoToStartPage();
                Debug.WriteLine("Create New Instance");

                }
            }
            
           
            
            // Set the background color
            // Matches Background.C from Syles.mcml
            //host.MediaCenterEnvironment.BackgroundColor = System.Drawing.Color.FromArgb(21, 35, 39);

                   
        }


        private static HistoryOrientedPageSession s_session;
    }
}

